/*
 * index Javascript
 */
var cardGroupInfo = parent.user_cardGroupInfo;

function getCardGroupInfo(){
	$.getJSON("get_business_card_group_info.do?_random="+(new Date()).getTime(),function(result){
		cardGroupInfo=result;
	});
}

$(document).ready(function(){
	//$("a:eq(0)","#sideUl").click();
});

function loadContainer108(url){
	$("#container108").html("");
	if(url.length == 0) return false;
	$("#container").attr("id","container108");
	if(url.split("?").length == 1){
		url+="?randomval="+ (new Date).getTime();
	} else if(url.split("?").length == 2){
		url+="&?randomval="+ (new Date).getTime();
	}
	$("#container108").load(url);
}
/*** 新增名片 ***/
//新增名片-实现
artDialog.newCard = function () {
	var newCardgroupHtml = '<tr><td><label class="grouplabel"><input type="radio" id="newCardInGroup" name="newCardInGroup" value="nogroup" />未分组</label><td>';
	if(cardGroupInfo.length > 0){
		for(var i = 0; i < cardGroupInfo.length; i ++){
			cardGroupInfo[i].groupname
			var temp ='<td><label class="grouplabel"><input type="radio" id="newCardInGroup" name="newCardInGroup" value="'+cardGroupInfo[i].id+'" />'+cardGroupInfo[i].groupname+'</label><td>';
			if(i%3==1){temp+='</tr>';}
			else if(i%3==2){temp='<tr>'+temp;}
			newCardgroupHtml+=temp;
		}
	}
    return artDialog({
        id: 'Prompt',
        title: '添加名片',
        opacity: .1,
        drag: false,
        resize: false,
        content: [
            '<div class="newCard_Group"><table>',
			newCardgroupHtml,
			'</table></div>',
            '<div class="newCard_Panel"><table>',
            '<tr><th>姓名</th><td><input class="text" type=text id="newCard_cardname"/></td><th>手机</th><td><input class="text"  id="newCard_mobilephone" type=text /></td></tr>',
            '<tr><th>电话</th><td><input class="text" type=text id="newCard_phone"/></td><th>性别</th><td><select id="newCard_sex"><option value="">请选择</option><option vlaue="男">男</option><option vlaue="女">女</option></select></td></tr>',
            '<tr><th>生日</th><td><input class="text" type=text id="newCard_birthday"/></td><th>公司</th><td><input class="text"  id="newCard_company" type=text /></td></tr>',
            '<tr></td><th>邮箱</th><td><input class="text"  id="newCard_mail" type=text /></td><th>职务</th><td><input class="text" type=text id="newCard_post"/></tr>',
            '<tr><th>婚否</th><td><select id="newCard_married"><option value="">请选择</option><option vlaue="已婚">已婚</option><option vlaue="未婚">未婚</option></select></td><th>邮编</th><td><input class="text"  id="newCard_zipcode" type=text /></td></tr>',
            '<tr><th>地址</th><td colspan=3><input class="long" type=text id="newCard_address" /></tr>',
            '<tr><th>备注1</th><td><input class="text" type=text id="newCard_remark1"/></td><th>备注2</th><td><input class="text" type=text id="newCard_remark2"/></td></tr>',
            '<tr><th>备注3</th><td><input class="text" type=text id="newCard_remark3"/></td><th>备注4</th><td><input class="text" type=text id="newCard_remark4"/></td></tr>',
            '</table>',
            '</div>'
            ].join(''),
        init: function () {
        },
        okVal:'添加',
        ok: function(){
        	var newCardInGroup = $('input[name=newCardInGroup][checked]').val(); 
        	var cardname = $("#newCard_cardname").val();
        	var mobilephone = $("#newCard_mobilephone").val();
			var phone = $("#newCard_phone").val();
			var sex = $("#newCard_sex").val();
			var birthday = $("#newCard_birthday").val();
			var company = $("#newCard_company").val();
			var post = $("#newCard_post").val();
			var mail = $("#newCard_mail").val();
			var married = $("#newCard_married").val();
			var zipcode = $("#newCard_zipcode").val();
			var address = $("#newCard_address").val();
			var remark1 = $("#newCard_remark1").val();
			var remark2 = $("#newCard_remark2").val();
			var remark3 = $("#newCard_remark3").val();
			var remark4 = $("#newCard_remark4").val();
        	this.button({name: '添加',disabled: true});
        	var checkStat = checkNewCard();
			if(checkStat == "success"){
				$.ajax({
				    url: 'business_card_addcard.do',
				    data:{newCardInGroup:newCardInGroup,cardname:cardname,mobilephone:mobilephone,phone:phone,sex:sex,birthday:birthday,company:company,post:post,mail:mail,married:married,zipcode:zipcode,address:address,remark1:remark1,remark2:remark2,remark3:remark3,remark4:remark4},
				    cache: false,
					error: function(){
						//alert('Error loading XML document');
						var list = art.dialog.list;
						for (var i in list) {
						    list[i].close();
						};
						art.dialog.tipsRed('添加失败，请稍后再试!');
					},
					success: function(data){
						var list = art.dialog.list;
						for (var i in list) {
						    list[i].close();
						};
						$("#submitSearch").click();
						art.dialog.tipsBlue('添加成功!');
					}
				});
				//art.dialog.tipsBlue('添加成功!');
        		return false;
        	} else {
        		art.dialog.tipsRed(checkStat);
        		this.button({name: '添加',disabled: false});
        		return false;
        	}
        },
        cancel: function(){}
    });
};

//新增名片
function newCard(){
	art.dialog.newCard();
	var cGid = $("#currentGroupId").val();
	if(cGid == null || cGid == "all")cGid = "nogroup";
    $("input[type=radio][value="+cGid+"]").attr("checked","checked");
}

//检查新增名片
function checkNewCard(){
	var name = $("#newCard_cardname").val();
	if(name == null || name == ""){
		return "请填写姓名";
	}
	var name = $("#newCard_cardname").val();
	if(name.length>9){
		return "姓名不超过9个字符";
	}
	var mobilephone = $("#newCard_mobilephone").val();
	if(mobilephone == null || mobilephone == ""){
		return "请填写手机";
	}
	if(mobilephone.length>20){
		return "手机号码不超过20个字符";
	}
	var mobilephone = $("#newCard_birthday").val();
	if(!IsDate(mobilephone))return "请输入正确的生日格式，如 1980-06-20";

	return "success";
}

/*** 删除名片 ***/
//删除名片-实现
artDialog.deleteCard = function (content,id) {
	return artDialog({
		id: 'Confirm',
		icon: 'question',
		opacity: .1,
		content: content,
		drag: false,
		ok: function () {
			$.ajax({
				url: "delete_business_card.do",
				data:{id:id},
				success: function(data){
					if(data=="success"){
						$("#submitSearch").click();
						art.dialog.tipsBlue('成功删除!');
					} else{
						art.dialog.tipsRed('数据异常!');
					}
					getCardGroupInfo();
				},
				error: function(){
					art.dialog.tipsRed('数据异常!');
				}
			});
		},
		cancel: true
	});
};
//删除名片
function deleteCard(){
	var id="";
	$(":checked","#datalistTbody").each(function (){
		var t_id= $(this).attr("id");
		if(t_id != null && t_id!=""){
			id+=",";
			id+=t_id;
		};
	});
	if(id==""){art.dialog.tipsRed('请选择需要删除的名片!'); return;}
	art.dialog.deleteCard('确定删除?',id);
}

function deleteCardDir(id,name){
	art.dialog.deleteCard('确定删除"'+name+'"?',id);
}
/*** 新建分组 ***/
//新增分组-实现
artDialog.newCardGroup = function () {
    return artDialog({
        id: 'Prompt',
        title: '添加分组',
        opacity: .1,
        drag: false,
        resize: false,
        content: [
            '<div class="newCard_Panel"><table>',
            '<tr><th style="width:70px">分组名称</th><td><input class="text" type=text id="newCardGroup_groupname"/></td></tr>',
            '</table>',
            '</div>'
            ].join(''),
        init: function () {
        },
        okVal:'添加',
        ok: function(){
        	var groupname = $("#newCardGroup_groupname").val();
        	if(groupname.length>9){
        		art.dialog.tipsRed("分组名称不得大于9个字符");
        		this.button({name: '添加',disabled: false});
        		return false;
        	}
        	for(var i = 0; i < cardGroupInfo.length; i++){
        		if(cardGroupInfo[i].groupname == groupname){
        			art.dialog.tipsRed("分组名称已存在");
	        		this.button({name: '添加',disabled: false});
	        		return false;
        		}
        	}
        	
			if(groupname != ""){
				this.button({name: '添加',disabled: true});
				$.ajax({
				    url: 'business_card_addcard_group.do',
				    data:{groupname:groupname},
				    cache: false,
					error: function(){
						//alert('Error loading XML document');
						var list = art.dialog.list;
						for (var i in list) {
						    list[i].close();
						};
						art.dialog.tipsRed('添加失败，请稍后再试!');
					},
					success: function(data){
						var list = art.dialog.list;
						for (var i in list) {
						    list[i].close();
						};
						parent.loadCardLeft();
						//$("#submitSearch").click();
						art.dialog.tipsBlue('添加成功!');
					}
				});
				getCardGroupInfo();
				//art.dialog.tipsBlue('添加成功!');
        		return false;
        	} else {
        		art.dialog.tipsRed("请填写分组名称");
        		this.button({name: '添加',disabled: false});
        		return false;
        	}
        },
        cancel: function(){}
    });
};

//新增分组
function newCardGroup(){
	art.dialog.newCardGroup();
}

/*** 删除分组 ***/
//删除分组-实现
artDialog.deleteCardGroup = function (content,currentGroupId) {
	return artDialog({
		id: 'Confirm',
		icon: 'question',
		opacity: .1,
		content: content,
		drag: false,
		ok: function () {
			$.ajax({
				url: "delete_business_card_group.do",
				data:{currentGroupId:currentGroupId},
				success: function(data){
					if(data=="success"){
						//var eid = "#group_li_"+currentGroupId;
						//$(eid).remove();
						//$("#link_group_all").click();
						parent.loadCardLeft();
						//art.dialog.tipsBlue('成功删除分组!');
					} else{
						art.dialog.tipsRed('数据异常!');
					}
					getCardGroupInfo();
				},
				error: function(){
					art.dialog.tipsRed('数据异常!');
					getCardGroupInfo();
				}
			});
		},
		cancel: true
	});
};
//删除分组
function deleteCardGroup(){
	if($("#currentGroupId").val() == null || $("#currentGroupId").val() == "" || $("#currentGroupId").val() == "nogroup" || $("#currentGroupId").val() == "all"){
		art.dialog.tipsRed('不可删除该分组!');
		return;
	}
	art.dialog.deleteCardGroup('确定删除本组?',$("#currentGroupId").val());
}

/*** 设置名片 ***/
//设置名片-实现
artDialog.editCard = function (id) {
	var newCardgroupHtml = '<tr><td><label class="grouplabel"><input type="radio" id="newCardInGroup" name="newCardInGroup" value="nogroup" />未分组</label><td>';
	if(cardGroupInfo.length > 0){
		for(var i = 0; i < cardGroupInfo.length; i ++){
			cardGroupInfo[i].groupname
			var temp ='<td><label class="grouplabel"><input type="radio" id="newCardInGroup" name="newCardInGroup" value="'+cardGroupInfo[i].id+'" />'+cardGroupInfo[i].groupname+'</label><td>';
			if(i%3==1){temp+='</tr>';}
			else if(i%3==2){temp='<tr>'+temp;}
			newCardgroupHtml+=temp;
		}
	}
    return artDialog({
        id: 'Prompt',
        title: '名片设置',
        opacity: .1,
        drag: false,
        resize: false,
        content: [
            '<div class="newCard_Group"><table>',
			newCardgroupHtml,
			'</table></div>',
            '<div class="newCard_Panel"><table>',
            '<tr><th>姓名</th><td><input class="text" type=text id="newCard_cardname"/></td><th>手机</th><td><input class="text"  id="newCard_mobilephone" type=text /></td></tr>',
            '<tr><th>电话</th><td><input class="text" type=text id="newCard_phone"/></td><th>性别</th><td><select id="newCard_sex"><option value="">请选择</option><option vlaue="男">男</option><option vlaue="女">女</option></select></td></tr>',
            '<tr><th>生日</th><td><input class="text" type=text id="newCard_birthday"/></td><th>公司</th><td><input class="text"  id="newCard_company" type=text /></td></tr>',
            '<tr></td><th>邮箱</th><td><input class="text"  id="newCard_mail" type=text /></td><th>职务</th><td><input class="text" type=text id="newCard_post"/></tr>',
            '<tr><th>婚否</th><td><select id="newCard_married"><option value="">请选择</option><option vlaue="已婚">已婚</option><option vlaue="未婚">未婚</option></select></td><th>邮编</th><td><input class="text"  id="newCard_zipcode" type=text /></td></tr>',
            '<tr><th>地址</th><td colspan=3><input class="long" type=text id="newCard_address" /></tr>',
            '<tr><th>备注1</th><td><input class="text" type=text id="newCard_remark1"/></td><th>备注2</th><td><input class="text" type=text id="newCard_remark2"/></td></tr>',
            '<tr><th>备注3</th><td><input class="text" type=text id="newCard_remark3"/></td><th>备注4</th><td><input class="text" type=text id="newCard_remark4"/></td></tr>',
            '</table>',
            '</div>'
            ].join(''),
        init: function () {
        	//getData
        	$.getJSON("get_business_card_info.do?id="+id+"&_random="+(new Date()).getTime(),function(result){
        		var cGid = result.cardInfo.cardgroup;
        		if(cGid == null || cGid == "")cGid = "nogroup";
        		$("input[type=radio][value="+cGid+"]").attr("checked","checked");
        		$("#newCard_cardname").val(result.cardInfo.cardname);
        		$("#newCard_mobilephone").val(result.cardInfo.mobilephone);
        		$("#newCard_phone").val(result.cardInfo.phone);
        		var cSex = result.cardInfo.sex;
        		$("#newCard_sex option[text="+cSex+"]").attr("selected", true);
        		$("#newCard_birthday").val(result.cardInfo.birthday);
        		$("#newCard_company").val(result.cardInfo.company);
        		$("#newCard_post").val(result.cardInfo.post);
        		$("#newCard_mail").val(result.cardInfo.mail);
        		var cMarried = result.cardInfo.married;
        		$("#newCard_married option[text="+cMarried+"]").attr("selected", true);
        		$("#newCard_married").val(result.cardInfo.married);
        		$("#newCard_zipcode").val(result.cardInfo.zipcode);
        		$("#newCard_address").val(result.cardInfo.address);
        		$("#newCard_remark1").val(result.cardInfo.remark1);
        		$("#newCard_remark2").val(result.cardInfo.remark2);
        		$("#newCard_remark3").val(result.cardInfo.remark3);
        		$("#newCard_remark4").val(result.cardInfo.remark4);
			});
        },
        okVal:'添加',
        ok: function(){
        	var newCardInGroup = $('input[name=newCardInGroup][checked]').val(); 
        	var cardname = $("#newCard_cardname").val();
        	var mobilephone = $("#newCard_mobilephone").val();
			var phone = $("#newCard_phone").val();
			var sex = $("#newCard_sex").val();
			var birthday = $("#newCard_birthday").val();
			var company = $("#newCard_company").val();
			var post = $("#newCard_post").val();
			var mail = $("#newCard_mail").val();
			var married = $("#newCard_married").val();
			var zipcode = $("#newCard_zipcode").val();
			var address = $("#newCard_address").val();
			var remark1 = $("#newCard_remark1").val();
			var remark2 = $("#newCard_remark2").val();
			var remark3 = $("#newCard_remark3").val();
			var remark4 = $("#newCard_remark4").val();
        	this.button({name: '添加',disabled: true});
        	var checkStat = checkNewCard();
			if(checkStat == "success"){
				$.ajax({
				    url: 'business_card_editcard.do',
				    data:{id:id,newCardInGroup:newCardInGroup,cardname:cardname,mobilephone:mobilephone,phone:phone,sex:sex,birthday:birthday,company:company,post:post,mail:mail,married:married,zipcode:zipcode,address:address,remark1:remark1,remark2:remark2,remark3:remark3,remark4:remark4},
				    cache: false,
					error: function(){
						//alert('Error loading XML document');
						var list = art.dialog.list;
						for (var i in list) {
						    list[i].close();
						};
						art.dialog.tipsRed('添加失败，请稍后再试!');
					},
					success: function(data){
						var list = art.dialog.list;
						for (var i in list) {
						    list[i].close();
						};
						$("#submitSearch").click();
						art.dialog.tipsBlue('修改成功!');
					}
				});
				//art.dialog.tipsBlue('添加成功!');
        		return false;
        	} else {
        		art.dialog.tipsRed(checkStat);
        		this.button({name: '添加',disabled: false});
        		return false;
        	}
        },
        cancel: function(){}
    });
};

//设置名片
function editCard(id){
	//var count = 0;
	//$(":checked","#datalistTbody").each(function (){
		//count+=1;
	//});
	//if(count == 0){art.dialog.tipsRed('请选择需要设置的名片!'); return;}
	//if(count > 1){art.dialog.tipsRed('请选择一张名片'); return;}
	//var id = $(":checked","#datalistTbody").attr("id");
	art.dialog.editCard(id);
}
